package com.htzz.collection.query;

import com.htzz.collection.config.SystemConfig;
import com.htzz.collection.core.mongo.QueryMongoTemplate;
import com.htzz.collection.domain.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import springfox.documentation.annotations.ApiIgnore;

import javax.annotation.Resource;

@Api(tags = "数据查询接口")
@RestController
@Slf4j
@RequestMapping("/api/")
public class MongoQueryController {

    @Resource
    private QueryMongoTemplate queryMongoTemplate;


    /**
     * 根据设备ID查询最近50条状态数据
     * @param
     * @return
     */
    @ApiOperation("据设备ID查询最近50条状态数据")
    @PostMapping("/getLast50StatusByDevId.ht")
    public ResultVO getLast50StatusByDevId(@Validated @RequestBody QueryParam1Dto dto){
        try {
            log.info("==========getLast50StatusByDevId========");
            return ResultVO.ok(queryMongoTemplate.getLast50StatusByDevId(dto));
        } catch (Exception e) {
            log.info("==========getLast50StatusByDevId 异常========");
            log.info(e.getMessage());
            e.printStackTrace();
            return ResultVO.error(SystemConfig.EXCEPTION);
        }
    }

    /**
     * 根据设备ID查询最近新状态数据
     * @param
     * @return
     */
    @ApiOperation("据设备ID查询最新状态数据")
    @PostMapping("/getLastStatusByDevId.ht")
    public ResultVO getLastStatusByDevId(@Validated @RequestBody QueryParam1Dto dto){
        try {
            log.info("==========getLastStatusByDevId========");
            return ResultVO.ok(queryMongoTemplate.getLastStatusByDevId(dto));
        } catch (Exception e) {
            log.info("==========getLastStatusByDevId 异常========");
            log.info(e.getMessage());
            e.printStackTrace();
            return ResultVO.error(SystemConfig.EXCEPTION);
        }
    }



    @ApiOperation("根据设备采集点和时间范围，分页查询数据")
    @PostMapping("getDatasByDevIdKTimesPage")
    public ResultVO getDatasByDevIdKTimesPage(@Validated @RequestBody QueryForDto1 query){
        try {
            log.info("==========getDatasByDevIdKTimesPage========");
            return ResultVO.ok(queryMongoTemplate.getDatasByDevIdColIdTimesPage(query));
        } catch (Exception e) {
            log.info("==========getDatasByDevIdKTimesPage 异常========");
            log.info(e.getMessage());
            e.printStackTrace();
//            Map<String,Object> rmap = new HashMap<>();
//            rmap.put(Constraint.SUCCESS,false);
//            rmap.put(Constraint.CODE, SystemConfig.EXCEPTION.getCode());
//            rmap.put(Constraint.DATA,SystemConfig.EXCEPTION.getMsg());
            return ResultVO.error(SystemConfig.EXCEPTION);
        }
    }

    @ApiOperation("查询设备采集点最新数据")
    @PostMapping("/getLastDatasByDevIdK")
    public ResultVO getLastDatasByDevIdK(@Validated @RequestBody QueryParam2Dto query){
        try {
            log.info("==========/getLastDatasByDevIdK========");
            return ResultVO.ok(queryMongoTemplate.getLastDatasByDevIdColId(query));
        } catch (Exception e) {
            log.info("==========/getLastDatasByDevIdK 异常========");
            log.info(e.getMessage());
            e.printStackTrace();
//            Map<String,Object> rmap = new HashMap<>();
//            rmap.put(Constraint.DATA,SystemConfig.EXCEPTION.getMsg());
//            rmap.put(Constraint.SUCCESS,false);
            return ResultVO.error(SystemConfig.EXCEPTION);
        }
    }

    @ApiOperation("按时间范围查询采集点历史数据")
    @PostMapping("/getHstoricalDateByKAndTime")
    public ResultVO getHstoricalDateByKAndTime(@Validated @RequestBody QueryForDto1 query){
        try {
            log.info("==========/getHstoricalDateByKAndTime========");
            return ResultVO.ok(queryMongoTemplate.getDatasByDevIdColIdTimesPage(query));
        } catch (Exception e) {
            log.info("==========/getHstoricalDateByKAndTime 异常========");
            log.info(e.getMessage());
//            Map<String,Object> rmap = new HashMap<>();
//            rmap.put(Constraint.DATA,SystemConfig.EXCEPTION.getMsg());
//            rmap.put(Constraint.SUCCESS,false);
            e.printStackTrace();
            return ResultVO.error(SystemConfig.EXCEPTION);
        }
    }


    /**
     * 根据设备id分页查询最新采集数据
     * @param query
     * @return
     */
    @ApiOperation("根据设备id分页获得实时采集数据")
    @PostMapping("/getDevDatasByDevIdPage")
    public ResultVO getDevDatasByDevIdPage(@Validated @RequestBody QueryParam3Dto query){
        try {
            log.info("==========/getDevDatasByDevIdPage========");
            return ResultVO.ok(queryMongoTemplate.getDevDatasByDevIdPage(query));
        } catch (Exception e) {
            log.info("==========/getDevDatasByDevIdPage 异常========");
            log.info(e.getMessage());
            e.printStackTrace();
            return ResultVO.error(SystemConfig.EXCEPTION);
        }
    }


    /**
     * 根据设备id获得30分钟内的状态数据
     * @param query
     * @return
     */
    @ApiOperation("根据设备id获得30分钟内的状态数据")
    @PostMapping("/getStatusByDevId")
    public ResultVO getStatusByDevId(@Validated @RequestBody QueryParam1Dto query){
        try {
            log.info("==========/getStatusByDevId========");
            return ResultVO.ok(queryMongoTemplate.getStatusByDevId(query));
        } catch (Exception e) {
            log.info("==========/getStatusByDevId 异常========");
            log.info(e.getMessage());
            e.printStackTrace();
            return ResultVO.error(SystemConfig.EXCEPTION);
        }
    }

    /**
     * 根据设备id获得最新一条采集数据
     * @param query
     * @return
     */
    @ApiOperation("根据设备id获得最新一条采集数据")
    @PostMapping("/getLastTimeByDevId")
    public ResultVO getLastTimeByDevId(@Validated @RequestBody QueryParam1Dto query){
        try {
            log.info("==========/getLastTimeByDevId========");
            return ResultVO.ok(queryMongoTemplate.getLastTimeByDevId(query));
        } catch (Exception e) {
            log.info("==========/getLastTimeByDevId 异常========");
            log.info(e.getMessage());
            e.printStackTrace();
            return ResultVO.error(SystemConfig.EXCEPTION);
        }
    }


    /**
     * 根据设备id,采集点获取24小时的数据（每30分钟一条）
     * @param query
     * @return
     */
    @ApiOperation("根据设备id,采集点获取24小时的数据（每30分钟一条）")
    @PostMapping("/getHstoricalData")
    public ResultVO getHstoricalData(@Validated @RequestBody QueryParam2Dto query){
        try {
            log.info("==========/getHstoricalData========");
            return ResultVO.ok(queryMongoTemplate.getHstoricalData(query));
        } catch (Exception e) {
            log.info("==========/getHstoricalData 异常========");
            log.info(e.getMessage());
            e.printStackTrace();
            return ResultVO.error(SystemConfig.EXCEPTION);
        }
    }


    /**
     * 根据设备id获取24小时的状态数据（每30分钟一条）
     * @param query
     * @return
     */
    @ApiOperation("根据设备id获取24小时的状态数据（每30分钟一条）")
    @PostMapping("/getHstoricalStatus")
    public ResultVO getHstoricalStatus(@Validated @RequestBody QueryParam1Dto query){
        try {
            log.info("==========/getHstoricalStatus========");
            return ResultVO.ok(queryMongoTemplate.getHstoricalStatus(query));
        } catch (Exception e) {
            log.info("==========/getHstoricalStatus 异常========");
            log.info(e.getMessage());
            e.printStackTrace();
            return ResultVO.error(SystemConfig.EXCEPTION);
        }
    }



    /**
     * 根据设备id,采集点查询指定小时内的数据（每30分钟一条）
     * @param query
     * @return
     */
    @ApiOperation("根据设备id,采集点查询指定小时内的数据（每30分钟一条）")
    @PostMapping("/getHstoricalDataByHours")
    public ResultVO getHstoricalDataByHours(@Validated @RequestBody QueryParam5Dto query){
        try {
            log.info("==========/getHstoricalDataByHours========");
            return ResultVO.ok(queryMongoTemplate.getHstoricalDataByHours(query));
        } catch (Exception e) {
            log.info("==========/getHstoricalDataByHours 异常========");
            log.info(e.getMessage());
            e.printStackTrace();
            return ResultVO.error(e.getMessage());
        }
    }


    /**
     * 根据设备id获取查询指定小时内状态数据（每30分钟一条）
     * @param query
     * @return
     */
    @ApiOperation("根据设备id获取查询指定小时内状态数据（每30分钟一条）")
    @PostMapping("/getHstoricalStatusByHours")
    public ResultVO getHstoricalStatusByHours(@Validated @RequestBody QueryParam4Dto query){
        try {
            log.info("==========/getHstoricalStatusByHours========");
            return ResultVO.ok(queryMongoTemplate.getHstoricalStatusByHours(query));
        } catch (Exception e) {
            log.info("==========/getHstoricalStatusByHours 异常========");
            log.info(e.getMessage());
            e.printStackTrace();
            return ResultVO.error(e.getMessage());
        }
    }

    /**
     * 分页查询指定日期的的设备数据
     * @param query
     * @return
     */
    @ApiOperation("分页查询指定日期的的设备数据")
    @PostMapping("/getDevDatasByDevIdDate")
    public ResultVO getDevDatasByDevIdDate(@Validated @RequestBody QueryParam6Dto query){
        try {
            log.info("==========/getDevDatasByDevIdDate========");
            return ResultVO.ok(queryMongoTemplate.getDevDatasByDevIdDate(query));
        } catch (Exception e) {
            log.info("==========/getDevDatasByDevIdDate 异常========");
            log.info(e.getMessage());
            e.printStackTrace();
            return ResultVO.error(e.getMessage());
        }
    }

    /**
     * 分页查询指定日期的的设备、采集点数据
     * @param query
     * @return
     */
    @ApiOperation("分页查询指定日期的的设备、采集点数据")
    @PostMapping("/getDatasByDevIdKDate")
    public ResultVO getDatasByDevIdKDate(@Validated @RequestBody QueryParam7Dto query){
        try {
            log.info("==========/getDatasByDevIdKDate========");
            return ResultVO.ok(queryMongoTemplate.getDevDatasByDevIdColIdDate(query));
        } catch (Exception e) {
            log.info("==========/getDatasByDevIdKDate 异常========");
            log.info(e.getMessage());
            e.printStackTrace();
            return ResultVO.error(e.getMessage());
        }
    }

    /**
     * 分页查询指定日期的的设备状态数据
     * @param query
     * @return
     */
    @ApiOperation("分页查询指定日期的的设备状态数据")
    @PostMapping("/getStatusByDevIdDate")
    public ResultVO getStatusByDevIdDate(@Validated @RequestBody QueryParam6Dto query){
        try {
            log.info("==========/getStatusByDevIdDate========");
            return ResultVO.ok(queryMongoTemplate.getStatusByDevIdDate(query));
        } catch (Exception e) {
            log.info("==========/getStatusByDevIdDate 异常========");
            log.info(e.getMessage());
            e.printStackTrace();
            return ResultVO.error(e.getMessage());
        }
    }

    @ApiIgnore(value = "因为v的string类型的比较方式存在歧义，所以先忽略此接口")
    @ApiOperation("分页查询时间范围内的指定设备、采集点大于指定值的数据")
    @PostMapping("/getDevHightDatasByDevIdDate")
    public ResultVO getDevHightDatasByDevIdDate(@Validated @RequestBody QueryParam8Dto query){
        try {
            log.info("==========/getDevHightDatasByDevIdDate========");
            return ResultVO.ok(queryMongoTemplate.getHightDatasByDevIdColId(query));
        } catch (Exception e) {
            log.info("==========/getDevHightDatasByDevIdDate 异常========");
            log.info(e.getMessage());
            e.printStackTrace();
            return ResultVO.error(e.getMessage());
        }
    }

    @ApiIgnore(value = "因为v的string类型的比较方式存在歧义，所以先忽略此接口")
    @ApiOperation("分页查询时间范围内的指定设备、采集点小于指定值的数据")
    @PostMapping("/getDevLowDatasByDevIdDate")
    public ResultVO getDevLowDatasByDevIdDate(@Validated @RequestBody QueryParam8Dto query){
        try {
            log.info("==========/getDevLowDatasByDevIdDate========");
            return ResultVO.ok(queryMongoTemplate.getLowDatasByDevIdColId(query));
        } catch (Exception e) {
            log.info("==========/getDevLowDatasByDevIdDate 异常========");
            log.info(e.getMessage());
            e.printStackTrace();
            return ResultVO.error(e.getMessage());
        }
    }


    @ApiOperation("分页查询时间范围内的指定设备、采集点指定值的数据")
    @PostMapping("/getDatasByDevIdKVDate")
    public ResultVO getDatasByDevIdKVDate(@Validated @RequestBody QueryParam8Dto query){
        try {
            log.info("==========/getDatasByDevIdKVDate========");
            return ResultVO.ok(queryMongoTemplate.getDatasByDevIdColIdV(query));
        } catch (Exception e) {
            log.info("==========/getDatasByDevIdKVDate 异常========");
            log.info(e.getMessage());
            e.printStackTrace();
            return ResultVO.error(e.getMessage());
        }
    }



    @ApiOperation("分页查询设备今日截止到当前时间运行状态数据信息")
    @PostMapping("/getRunStatusByDevIdToday")
    public ResultVO getRunStatusByDevIdToday(@Validated @RequestBody QueryParam3Dto query){
        try {
            log.info("==========/getRunStatusByDevIdToday========");
            return ResultVO.ok(queryMongoTemplate.getRunStatusByDevIdToday(query));
        } catch (Exception e) {
            log.info("==========/getRunStatusByDevIdToday 异常========");
            log.info(e.getMessage());
            e.printStackTrace();
            return ResultVO.error(e.getMessage());
        }
    }

    @ApiOperation("分页查询设备今日截止到当前时间待机状态数据信息")
    @PostMapping("/getStandbyStatusByDevIdToday")
    public ResultVO getStandbyStatusByDevIdToday(@Validated @RequestBody QueryParam3Dto query){
        try {
            log.info("==========/getStandbyStatusByDevIdToday========");
            return ResultVO.ok(queryMongoTemplate.getStandbyStatusByDevIdToday(query));
        } catch (Exception e) {
            log.info("==========/getStandbyStatusByDevIdToday 异常========");
            log.info(e.getMessage());
            e.printStackTrace();
            return ResultVO.error(e.getMessage());
        }
    }

    @ApiOperation("分页查询设备今日截止到当前时间故障状态数据信息")
    @PostMapping("/getFaultStatusByDevIdToday")
    public ResultVO getFaultStatusByDevIdToday(@Validated @RequestBody QueryParam3Dto query){
        try {
            log.info("==========/getFaultStatusByDevIdToday========");
            return ResultVO.ok(queryMongoTemplate.getFaultStatusByDevIdToday(query));
        } catch (Exception e) {
            log.info("==========/getFaultStatusByDevIdToday 异常========");
            log.info(e.getMessage());
            e.printStackTrace();
            return ResultVO.error(e.getMessage());
        }
    }

    @ApiOperation("分页查询设备今日截止到当前时间离线状态数据信息")
    @PostMapping("/getOfflineStatusByDevIdToday")
    public ResultVO getOfflineStatusByDevIdToday(@Validated @RequestBody QueryParam3Dto query){
        try {
            log.info("==========/getOfflineStatusByDevIdToday========");
            return ResultVO.ok(queryMongoTemplate.getOfflineStatusByDevIdToday(query));
        } catch (Exception e) {
            log.info("==========/getOfflineStatusByDevIdToday 异常========");
            log.info(e.getMessage());
            e.printStackTrace();
            return ResultVO.error(e.getMessage());
        }
    }


    @ApiOperation("分页查询设备指定日期运行状态数据信息")
    @PostMapping("/getRunStatusByDevIdDate")
    public ResultVO getRunStatusByDevIdDate(@Validated @RequestBody QueryParam6Dto query){
        try {
            log.info("==========/getRunStatusByDevIdDate========");
            return ResultVO.ok(queryMongoTemplate.getRunStatusByDevIdDate(query));
        } catch (Exception e) {
            log.info("==========/getRunStatusByDevIdDate 异常========");
            log.info(e.getMessage());
            e.printStackTrace();
            return ResultVO.error(e.getMessage());
        }
    }

    @ApiOperation("分页查询设备指定日期待机状态数据信息")
    @PostMapping("/getStandbyStatusByDevIdDate")
    public ResultVO getStandbyStatusByDevIdDate(@Validated @RequestBody QueryParam6Dto query){
        try {
            log.info("==========/getStandbyStatusByDevIdDate========");
            return ResultVO.ok(queryMongoTemplate.getStandbyStatusByDevIdDate(query));
        } catch (Exception e) {
            log.info("==========/getStandbyStatusByDevIdDate 异常========");
            log.info(e.getMessage());
            e.printStackTrace();
            return ResultVO.error(e.getMessage());
        }
    }

    @ApiOperation("分页查询设备指定日期故障状态数据信息")
    @PostMapping("/getFaultStatusByDevIdDate")
    public ResultVO getFaultStatusByDevIdDate(@Validated @RequestBody QueryParam6Dto query){
        try {
            log.info("==========/getFaultStatusByDevIdDate========");
            return ResultVO.ok(queryMongoTemplate.getFaultStatusByDevIdDate(query));
        } catch (Exception e) {
            log.info("==========/getFaultStatusByDevIdDate 异常========");
            log.info(e.getMessage());
            e.printStackTrace();
            return ResultVO.error(e.getMessage());
        }
    }

    @ApiOperation("分页查询设备指定日期离线状态数据信息")
    @PostMapping("/getOfflineStatusByDevIdDate")
    public ResultVO getOfflineStatusByDevIdDate(@Validated @RequestBody QueryParam6Dto query){
        try {
            log.info("==========/getOfflineStatusByDevIdDate========");
            return ResultVO.ok(queryMongoTemplate.getOfflineStatusByDevIdDate(query));
        } catch (Exception e) {
            log.info("==========/getOfflineStatusByDevIdDate 异常========");
            log.info(e.getMessage());
            e.printStackTrace();
            return ResultVO.error(e.getMessage());
        }
    }


    @ApiOperation("分页查询设备指定小时数内运行状态数据信息")
    @PostMapping("/getRunStatusByDevIdHours")
    public ResultVO getRunStatusByDevIdHours(@Validated @RequestBody QueryParam9Dto query){
        try {
            log.info("==========/getRunStatusByDevIdHours========");
            return ResultVO.ok(queryMongoTemplate.getRunStatusByDevIdHours(query));
        } catch (Exception e) {
            log.info("==========/getRunStatusByDevIdHours 异常========");
            log.info(e.getMessage());
            e.printStackTrace();
            return ResultVO.error(e.getMessage());
        }
    }

    @ApiOperation("分页查询设备指定小时数内待机状态数据信息")
    @PostMapping("/getStandbyStatusByDevIdHours")
    public ResultVO getStandbyStatusByDevIdHours(@Validated @RequestBody QueryParam9Dto query){
        try {
            log.info("==========/getStandbyStatusByDevIdHours========");
            return ResultVO.ok(queryMongoTemplate.getStandbyStatusByDevIdHours(query));
        } catch (Exception e) {
            log.info("==========/getStandbyStatusByDevIdHours 异常========");
            log.info(e.getMessage());
            e.printStackTrace();
            return ResultVO.error(e.getMessage());
        }
    }

    @ApiOperation("分页查询设备指定小时数内故障状态数据信息")
    @PostMapping("/getFaultStatusByDevIdHours")
    public ResultVO getFaultStatusByDevIdHours(@Validated @RequestBody QueryParam9Dto query){
        try {
            log.info("==========/getFaultStatusByDevIdHours========");
            return ResultVO.ok(queryMongoTemplate.getFaultStatusByDevIdHours(query));
        } catch (Exception e) {
            log.info("==========/getFaultStatusByDevIdDate 异常========");
            log.info(e.getMessage());
            e.printStackTrace();
            return ResultVO.error(e.getMessage());
        }
    }

    @ApiOperation("分页查询设备指定小时数内离线状态数据信息")
    @PostMapping("/getOfflineStatusByDevIdHours")
    public ResultVO getOfflineStatusByDevIdHours(@Validated @RequestBody QueryParam9Dto query){
        try {
            log.info("==========/getOfflineStatusByDevIdHours========");
            return ResultVO.ok(queryMongoTemplate.getOfflineStatusByDevIdHours(query));
        } catch (Exception e) {
            log.info("==========/getOfflineStatusByDevIdHours 异常========");
            log.info(e.getMessage());
            e.printStackTrace();
            return ResultVO.error(e.getMessage());
        }
    }


    @ApiOperation("根据设备ID查询今日设备数据条数")
    @PostMapping("/getDatasNumByDevIdToday")
    public ResultVO getDatasNumByDevIdToday(@Validated @RequestBody QueryParam1Dto query){
        try {
            log.info("==========/getDatasNumByDevIdToday========");
            return ResultVO.ok(queryMongoTemplate.getDatasNumByDevIdToday(query));
        } catch (Exception e) {
            log.info("==========/getDatasNumByDevIdToday 异常========");
            log.info(e.getMessage());
            e.printStackTrace();
            return ResultVO.error(e.getMessage());
        }
    }

    @ApiOperation("根据设备ID、采集点ID查询今日设备数据条数")
    @PostMapping("/getDatasNumByDevIdKToday")
    public ResultVO getDatasNumByDevIdKToday(@Validated @RequestBody QueryParam2Dto query){
        try {
            log.info("==========/getDatasNumByDevIdKToday========");
            return ResultVO.ok(queryMongoTemplate.getDatasNumByDevIdColIdToday(query));
        } catch (Exception e) {
            log.info("==========/getDatasNumByDevIdKToday 异常========");
            log.info(e.getMessage());
            e.printStackTrace();
            return ResultVO.error(e.getMessage());
        }
    }
}


















